import Vue from "vue";
import VueRouter from "vue-router";

import Home from "../views/Home.vue";
import Category from "../views/Category.vue";
import Shopcart from "../views/Shopcart.vue";
import User from "../views/User.vue";
import Login from "../views/User/Login.vue";
import Register from "../views/User/Register.vue";
import GoodsList from "../views/goods/GoodsList.vue";
import GoodsInfo from "../views/goods/GoodsInfo.vue";
import Address from "../views/User/Address.vue";
import AddressEdit from "../views/User/AddressEdit.vue";
import AddressAdd from "../views/User/AddressAdd.vue";
import OrderList from "../views/order/OrderList.vue";
import OrderCreate from "../views/order/OrderCreate.vue";
import OrderShow from "../views/order/OrderShow.vue";
Vue.use(VueRouter);

//解决路由地址重复报错

const routes = [
  {
    path: "/",
    redirect: "/home",
    meta: { title: "首页" },
  },
  {
    path: "/home",
    component: Home,
    name: "home",
    meta: { title: "首页" },
  },
  {
    path: "/category",
    component: Category,
    name: "category",
    meta: { title: "分类" },
  },
  {
    path: "/shopcaty",
    component: Shopcart,
    name: "shopcart",
    meta: { title: "购物车" },
  },
  {
    path: "/user",
    component: User,
    name: "user",
    meta: { title: "我的" },
  },
  {
    path: "/user/login",
    component: Login,
    name: "login",
    meta: { title: "登录" },
  },
  {
    path: "/goodslist/:category_id",
    component: GoodsList,
    props: true,
    name: "goods_list",
    meta: { title: "商品列表" },
  },
  {
    path: "/goodsinfo/:id",
    component: GoodsInfo,
    props: true,
    name: "goods_info",
    meta: { title: "商品信息" },
  },
  {
    path: "/user/address",
    component: Address,
    name: "address",
    meta: { title: "收货地址" },
  },
  {
    path: "/user/address/add",
    component: AddressAdd,
    name: "address_add",
    meta: { title: "新增收货地址" },
  },
  {
    path: "/user/address/edit",
    component: AddressEdit,
    name: "address_edit",
    meta: { title: "修改收货地址" },
  },
  {
    path: "/order/list",
    component: OrderList,
    name: "order_list",
    meta: { title: "我的订单" },
  },
  {
    path: "/order/create",
    component: OrderCreate,
    name: "order_create",
    meta: { title: "下订单" },
  },
  {
    path: "/user/address/select",
    component: Address,
    name: "address_select",
    meta: { title: "选择收货地址" },
  },
  {
    path: "/order/show",
    component: OrderShow,
    props: true,
    name: "order_show",
    meta: { title: "查看订单" },
  },
  {
    path: "/user/register",
    component: Register,
    name: "register",
    meta: { title: "注册" },
  },
];
const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  routes,
  linkActiveClass: "active",
});
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  next();
});

export default router;
